package com.bank.manage.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.bank.manage.constance.BankConstance;
import com.bank.manage.mapper.BankCheckRecordsMapper;
import com.bank.manage.model.BankCheckRecords;
import com.bank.manage.model.param.BankQueryParam;
import com.bank.manage.model.vo.RecordsVo;
import com.bank.manage.service.BankCheckRecordsService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.gavin.core.constant.CommonConst;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 管理操作记录 服务实现类
 * </p>
 *
 * @author grl
 * @since 2024-04-01
 */
@Service
@RequiredArgsConstructor
public class BankCheckRecordsServiceImpl extends ServiceImpl<BankCheckRecordsMapper, BankCheckRecords> implements BankCheckRecordsService {

    private final HttpServletRequest request;

    @Override
    public Boolean addCheckRecord(RecordsVo vo, BankQueryParam param) {
        BankCheckRecords bankCheckRecords = new BankCheckRecords();
        addAccessInfo(bankCheckRecords);
        LocalDateTime now = LocalDateTime.now();
        bankCheckRecords.setWalletNum(param.getWalletNum());
        bankCheckRecords.setBankCheckNum(param.getRequestId());
        bankCheckRecords.setResponseJson(JSON.toJSONString(vo));
        bankCheckRecords.setRequestJson(JSON.toJSONString(param));
        bankCheckRecords.setCheckTime(now);
        bankCheckRecords.setId(null);
        bankCheckRecords.setCreateTime(now);
        bankCheckRecords.setVerificationRemark(param.getRemark());
        bankCheckRecords.setBankResponse(vo.getResult() ? CommonConst.ONE:CommonConst.ZERO);
        return save(bankCheckRecords);
    }

    private void addAccessInfo(BankCheckRecords record){
        LocalDateTime now = LocalDateTime.now();
        record.setSafetyKey(request.getHeader(CommonConst.SAFETY_KEY));
        record.setVerificationWay(request.getHeader(CommonConst.SAFETY_TYPE));
        record.setVerificationResult(BankConstance.SUCCESS);
        record.setKeyVerificationResult(BankConstance.SUCCESS);
        record.setSignatureVerificationResult(BankConstance.SUCCESS);
        record.setOperationResults(BankConstance.OPT_SUCCESS);
        record.setVerificationTime(now);
    }
}
